<?
	/* PHP Twitter Feed Importer
@mradamdavies
www.elevatelocal.co.uk
==---------------------------------*/
 
class pull_tweets { // Create a basic class
 
var $twitter_handle; // Twitter username
var $tweet_limit; // Max tweets to return
var $tweet_links; // Show @profile links
var $tweet_tags; // Show #tag links
var $tweet_avatar; // Show twitter avatar
var $tweet_profile; // Show twitter profile link
 
var $hard_max; // Real max tweets (Used to filter out @replies)
var $i; // Tweet counter
 
function show_tweet_links($tweet_links_output, $tweet_links) { // Find and replace @replies with links
if($tweet_links == true) {
$find = '/\@([a-zA-Z]+)/';
$replace = '<a class="hash_link" href="http://twitter.com/'.strtolower('\1').'" rel="nofollow">@\1</a>';
$tweet_links_text = preg_replace($find,$replace,$tweet_links_output);
return $tweet_links_text;
}
else { return $tweet_links_output; }
}
 
function show_hash_tags($tweet_hashtags_output, $tweet_tags) { // Find and replace #tags with links
if($tweet_tags == true) {
$find_hash_tag = '/\#([a-zA-Z]+)/';
$replace_hash_tag = '<a class="hash_link" href="http://twitter.com/search?q=%23'.strtolower('\1').'" rel="nofollow">#\1</a>';
return preg_replace($find_hash_tag,$replace_hash_tag,$tweet_hashtags_output);
}
else { return $tweet_hashtags_output; }
}
 
function show_twitter_avatar($tweet_avatar_output, $tweet_avatar) { // Show avatar
if($tweet_avatar == true) {
return '<img src="'.$tweet_avatar_output.'" alt="" />';
}
else { return false; }
}
 
function show_twitter_profile_link($tweet_profile_output, $tweet_profile) { // Insert profile link
if($tweet_profile == true) {
return '<a class="profile_link" href="http://twitter.com/'.$tweet_profile_output.'">@'.$tweet_profile_output.'</a>';
}
else { return false; }
}
 
// Create a basic function to pull latest tweets
function tweets($twitter_handle, $tweet_limit, $tweet_links, $tweet_tags, $tweet_avatar, $tweet_profile) {
 
/* Store Tweets in a JSON object */
$tweet_feed = json_decode(file_get_contents('http://api.twitter.com/1/statuses/user_timeline.json?screen_name='.
$twitter_handle.'&exclude_replies=false&count='.$hard_max));
 
$i = 0; // Start the tweet limit counter
foreach ($tweet_feed as $tweet) { // Loop through the tweets
 
if ($tweet->in_reply_to_user_id == '' && $i != $tweet_limit) { // Don't show direct @replies
++$i; // Increase tweet counter
 
// Check true/false flags and return output accoringly
$tweet_text = $tweet->text;
$tweet_text = pull_tweets::show_tweet_links($tweet_text,$tweet_links); // Show @reply links?
$tweet_text = pull_tweets::show_hash_tags($tweet_text,$tweet_tags); // Show #tag links?
$twitter_avatar = pull_tweets::show_twitter_avatar($tweet->user->profile_image_url,$tweet_avatar); // Show avatar?
$twitter_profile = pull_tweets::show_twitter_profile_link($twitter_handle,$tweet_profile); // Show main profile link?
 
// Echo our tweet contents
echo '
<div class="tweet_bg">',$twitter_avatar,'<p>'.$tweet_text.'<br />'.$twitter_profile.'</p></div>';
}
 
} // end of foreach
 
} // end of tweets function
 
} // end of pull_tweets class

?>